package cn.sdstudio.controller;
import cn.sdstudio.model.Result;
import cn.sdstudio.entity.ScenicSpot;
import cn.sdstudio.service.ScenicSpotService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/scenic")
@Api(tags = "景点接口")
public class    ScenicSpotController {
    @Autowired
    private ScenicSpotService scenicSpotService;
    @ApiOperation("查询景点列表")
    @GetMapping("/lists")
    public Result<List<ScenicSpot>> getScenicSpot() {
        List<ScenicSpot> list = scenicSpotService.list();
        return Result.success(list);
    }

    @ApiOperation("分页查询景点列表")
    @GetMapping("/list")
    public Result<Page<ScenicSpot>> getScenicSpot(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "8") Integer pageSize) {
        Page<ScenicSpot> page = new Page<>();
        page.setCurrent(pageNo).setSize(pageSize);
        Page<ScenicSpot> scenicSpotPage = scenicSpotService.page(page);
        return Result.success(scenicSpotPage);
    }

    @ApiOperation("根据id获取景点详情")
    @GetMapping("/{id}")
    public Result<ScenicSpot> getScenicSpotById(@PathVariable(name = "id") Long id) {
        return scenicSpotService.queryScenicSpotById(id);
    }
    @ApiOperation("根据id进行修改")
    @PostMapping("/update")
    public Result updateScenicSpot(ScenicSpot scenicSpot) {
        boolean flag = scenicSpotService.updateById(scenicSpot);
        //清除redis缓存，解决数据一致性
        return Result.success(flag);
    }

    @ApiOperation("根据id删除")
    @GetMapping("/delete/{id}")
    public Result deleteScenicSpotById(@PathVariable(name = "id") Long id) {
        return Result.success(scenicSpotService.removeById(id));
    }
}
